package service;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import model.LogisticsRequirement;
import model.vo.QueryRequirementVO;
import utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class LogisticsRequirementService {
    
    // 添加物流要求
    public void addRequirement(LogisticsRequirement requirement) {
        DBUtil.insertLogisticsRequirement(requirement);
    }

    // 获取所有物流要求
    public ObservableList<QueryRequirementVO> getAllRequirements() {
        List<LogisticsRequirement> requirements = DBUtil.getAllLogisticsRequirements();
        // 将LogisticsRequirement列表转换为QueryRequirementDTO列表
        List<QueryRequirementVO> dtoList = IntStream.range(0, requirements.size())
                .mapToObj(i -> {
                    LogisticsRequirement req = requirements.get(i);
                    QueryRequirementVO vo = new QueryRequirementVO();
                    vo.setSerial_no(req.getSerialNo());  // 从1开始的自增序号
                    vo.setSerial_no_str(String.valueOf(i + 1)); // 字符串形式的序号
                    vo.setItem(req.getItem());
                    vo.setRequirement(req.getRequirement());
                    vo.setProEffi(req.getProEffi());
                    vo.setResponsiblePerson(req.getResponsiblePerson());
                    vo.setSupervisor(req.getSupervisor());
                    return vo;
                })
                .collect(Collectors.toList());
        return FXCollections.observableArrayList(dtoList);
    }

    // 删除物流要求
    public void deleteRequirement(int serialNo) {
        DBUtil.deleteLogisticsRequirement(serialNo);
    }

    //根据id查询
    public LogisticsRequirement getRequirementByNo(int serialNo) {
        LogisticsRequirement requirement = DBUtil.getLogisticsRequirementByNo(serialNo);
        return requirement;
    }

    //根据id修改
    public void updateRequirement(LogisticsRequirement requirement) {

        DBUtil.updateLogisticsRequirement(requirement);
    }
} 